package com.tgy.algorithm.base._并行计算;



public class _岛问题 {

    public static int isLandOfNum(int[][] nums) {

        int row = nums.length;
        int col = nums[0].length;

        int count = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (nums[i][j] == 1) {
                    infect(nums,i,j);
                    count++;
                }
            }
        }
        return count;
    }

    public static void infect(int[][] nums,int x,int y) {
        int row = nums.length;
        int col = nums[0].length;

        if (x < 0 || x >= row || y < 0 || y >= col || nums[x][y] != 1) {
            return;
        }

        nums[x][y] = 2;
        infect(nums,x-1,y);
        infect(nums,x+1,y);
        infect(nums,x,y-1);
        infect(nums,x,y+1);
    }

    public static void main(String[] args) {
        int[][] nums = {
                {0,0,0,1,0,1,0,0},
                {0,0,1,1,0,1,1,0},
                {0,0,1,1,0,1,0,0},
                {0,0,1,1,0,1,1,0},
                {0,0,0,0,0,0,0,0},
        };
        int landOfNum = isLandOfNum(nums);
        System.out.println(landOfNum);

        // 并查集
    }
}
